<?php

namespace App\Services;

use App\Admin\Controllers\AdminController;
use App\Enums\DataPermission;
use App\Helpers\KeyHandler;
use App\Models\Customer;
use App\Models\ReturnPerformance;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class AdminService
{


    public static function returnDealData(string $key, int $days, int $data_permission, int $user_id)
    {
        Cache::forget($key);
        return Cache::remember($key, now()->endOfDay(), function () use ($days, $data_permission, $user_id) {
            $end_day = now()->subDay()->endOfDay();
            $data = ReturnPerformance::query()
                ->when($data_permission == DataPermission::SELF, function (Builder $builder) use ($user_id) {
                    $builder->where('user_id', '=', $user_id);
                })
                ->whereBetween('created_at', [now()->subDays($days)->startOfDay(), $end_day])
                ->selectRaw("DATE_FORMAT( created_at, '%Y-%m-%d' ) created_at,user_id")->groupBy(['created_at', 'user_id'])
                ->get();
            $list = [];
            for ($i = $days; $i > 0; $i--) {
                $list[] = $data->whereBetween('created_at', [now()->subDays($i)->startOfDay(), now()->subDays($i)->endOfDay()])->count();
            }

            return [
                'list' => $list,
                'total' => count($data)
            ];
        });
    }

    public static function saleDealData(string $key, int $days, int $data_permission, int $user_id)
    {
        return Cache::remember($key, now()->endOfDay(), function () use ($days, $data_permission, $user_id) {
            $end_day = now()->subDay()->endOfDay();
            $data = Customer::query()
                ->when($data_permission == DataPermission::SELF, function (Builder $builder) use ($user_id) {
                    $builder->where('sale_user_id', '=', $user_id);
                })
                ->whereNotNull('deal')
                ->whereBetween('deal', [now()->subDays($days)->startOfDay(), $end_day])
                ->select('deal')->get();

            $list = [];
            for ($i = $days; $i > 0; $i--) {
                $list[] = $data->whereBetween('deal', [now()->subDays($i)->startOfDay(), now()->subDays($i)->endOfDay()])->count();
            }

            return [
                'list' => $list,
                'total' => count($data)
            ];
        });
    }



    //统计销售/招商成交 额
    public static function adminHomePersonalData(Model $model, string $key, int $days, int $data_permission, int $user_id)
    {
        return Cache::remember($key, now()->endOfDay(), function () use ($days, $model, $data_permission, $user_id) {
            $end_day = now()->subDay()->endOfDay();
            $user = AdminController::getUserInfo();
            $data = $model::query()
                ->when($data_permission == DataPermission::SELF, function (Builder $builder) use ($user_id) {
                    $builder->where('user_id', '=', $user_id);
                })
                ->whereBetween('created_at', [now()->subDays($days)->startOfDay(), $end_day])
                ->select(['money', 'created_at'])->get();
            $list = [];
            for ($i = $days; $i > 0; $i--) {
                $list[] = $data->whereBetween('created_at', [now()->subDays($i)->startOfDay(), now()->subDays($i)->endOfDay()])->sum('money');
            }
            return [
                'list' => $list,
                'total' => array_sum($list)
            ];
        });
    }


    //统计客户录入
    public static function adminHomeCustomerData(int $days, int $data_permission, int $user_id)
    {
        $key = $data_permission == DataPermission::ALL ? sprintf(KeyHandler::HOME_CUSTOMER, $days) : sprintf(KeyHandler::HOME_CUSTOMER_PERSONAL, $days, $user_id);
        return Cache::remember($key, now()->endOfDay(), function () use ($days, $data_permission, $user_id,) {
            $end_day = now()->subDay()->endOfDay();
            $data = Customer::query()
                ->when($data_permission == DataPermission::SELF, function (Builder $builder) use ($user_id) {
                    $builder->where('reception_user_id', '=', $user_id);
                })
                ->whereBetween('created_at', [now()->subDays($days)->startOfDay(), $end_day])
                ->select('created_at')->get();
            $list = [];
            for ($i = $days; $i > 0; $i--) {
                $list[] = $data->whereBetween('created_at', [now()->subDays($i)->startOfDay(), now()->subDays($i)->endOfDay()])->count();
            }
            return [
                'list' => $list,
                'total' => count($data)
            ];
        });
    }

}
